package sng.modules.view.components.city.strategy
{
	import flash.events.TimerEvent;
	
	import mx.controls.Alert;
	
	import sng.data.info.GlobalInfo;
	import sng.data.info.GlobalTimer;
	import sng.modules.model.vo.BuildingVO;
	import sng.modules.model.vo.UserBuildingVO;
	import sng.modules.view.components.city.logic.unit.BaseBuilding;
	import sng.modules.view.components.city.state.BuildGatherState;
	import sng.modules.view.components.city.state.BuildSupplyGoodsState;
	import sng.modules.view.components.city.state.BuildSupplyServicesState;
	import sng.modules.view.components.city.state.EnumBuildState;
	import sng.utils.TimeUtil;
	
	/**
	 * 
	 * @author leonliu
	 * 商店建筑的策略类
	 * 
	 */	
	public class BusinessStrategy extends Strategy
	{
		public function BusinessStrategy(building:BaseBuilding,bvo:BuildingVO, userbvo:UserBuildingVO)
		{
			super(building,bvo, userbvo);
		}
		
		public override function createBuildingStrategy():BuildingIncrement
		{
			//1.计算新建开始的数值变化
			GlobalInfo.mainvo.money=GlobalInfo.mainvo.money-m_bvo.buildingprice;
						
			return new BuildingIncrement();
		}
		
		public override function addBuildingStrategy():void
		{
			
			//1.计算新建后的数值变化
			GlobalInfo.mainvo.money=GlobalInfo.mainvo.money-m_bvo.buildingprice;
			//2.新建后建筑变成等待供货状态
			//判读是否是该建筑的第一次修建，如果是，弹出改名的面板
//			m_building.city.setBusinessBuildingName(m_bvo);
			
			if (m_userbvo.isIdle)
			{
				m_userbvo.nextState=EnumBuildState.SUPPLYGOODS_STATE;
				buildStateManage(EnumBuildState.IDLE_STATE);
				m_building.itempstate=EnumBuildState.SUPPLYGOODS_STATE;
			}
			else
			{
				buildStateManage(EnumBuildState.SUPPLYGOODS_STATE);
			}
		}
		
		/**
		 *供货后触化此方法 
		 * 
		 */		
		public override function supplyGoodsBuildingStrategy():BuildingIncrement
		{
			//0.先判断是否有足够多的货物
			if (GlobalInfo.mainvo.goods<m_bvo.needGoods)//货物消耗的配置还没有
			{
				Alert.show("货物不够");
				return null;
			}
			buildStateManage(EnumBuildState.SUPPLYSERVICES_STATE);
			//1.计算新建后的数值变化
			GlobalInfo.mainvo.goods-=m_bvo.needGoods;//每次补10个货物
			
			//3.向队列插入一条提供服务状态的动画,实时
			
			//4.向队列插入一条服务状态结束，转到收获金钱状态
			GlobalTimer.timeFunctionList.addItem({id:m_userbvo.buildingid,time:m_building.vo.countdown, func:gatherTimerHandler,run:true});
			
			//5.将状态保存到数据结构中 （要发到后台）
			m_userbvo.nextState=EnumBuildState.GATHER_STATE;//保存下一个状态
			m_userbvo.countdownTime=TimeUtil.getTimeSecond()+m_userbvo.countdown;
			return new BuildingIncrement();
		}
		
		/**
		 *收获金钱后触化此方法 
		 * 
		 */		
		public override function gatherBuildingStrategy():Boolean
		{
			if (super.gatherBuildingStrategy())
			{
				//2.修改状态为等待供货状态
				
				
				return true;
			}
			else
				
				return false;
		}
		
		/**
		 *收获后出现进度条，进度条完成后才能转换状态 
		 * 
		 */		
		public override function gatherProcessLater():void
		{
			buildStateManage(EnumBuildState.SUPPLYGOODS_STATE);
		}
		
		/**
		 *商业类建筑只有收获金钱需要倒计时
		 * 
		 */		
		public override function stateTimerHandler():void
		{
			gatherTimerHandler();
		}
		
		/**
		 *服务状态结束，转到收获状态
		 * 
		 */		
		private function gatherTimerHandler():void
		{
			buildStateManage(EnumBuildState.GATHER_STATE);
		}
		
		/**
		 *心跳 服务状态的心跳
		 * 
		 */		
		protected override function heartBeat(event:TimerEvent):void
		{
			if(tipFrame!=null)
				tip.gotoAndStop(tipFrame);
			switch(m_building.currentState.getiStateType())
			{
				case EnumBuildState.SUPPLYSERVICES_STATE:
				{
					var time:Number = TimeUtil.getTimeSecond();
					
					tip.timesText.text = (m_building.vo.countdown-GlobalTimer.getFuncTime(m_userbvo.buildingid))+"/"+m_building.vo.countdown;// (time-m_userbvo.countdownTime)+"/"+m_bvo.countdown;
					if(GlobalTimer.getFuncTime(m_userbvo.buildingid)<=0)//(time-m_userbvo.countdownTime>=m_bvo.countdown)
					{		
						//tip.timeText.text ="";
						GlobalTimer.removeHeartBeatEventListener(heartBeat);//移除监听事件
					}					
					break;
				}
//				case EnumBuildState.GATHER_STATE:
//				{
//					tip.servedText.text = m_bvo.countdown+"/"+m_bvo.countdown;
//					break;
//				}
				default:
				{
					
				}
			}
			
		}
		
		/**
		 * 
		 * @param iState
		 * 商业建筑各状态下的UI管理
		 * 
		 */		
		public override function buildStateManage(iState:uint):void
		{
			m_building.removeTip();
			if (iState!=EnumBuildState.IDLE_STATE)
			{
				//if (m_building.currentState&& m_building.currentState.getiStateType()==EnumBuildState.IDLE_STATE)
				
				GlobalTimer.setFuncRun(m_userbvo.buildingid,true);
			}
	
			m_building.setBuildState(EnumBuildState.getStateById(iState));
			switch (iState)
			{
				
				case EnumBuildState.NEW_STATE://创建
					tipFrame="building_create";
					break;
				case EnumBuildState.GATHER_STATE://收获
					tipFrame="business_getCoin";
					m_building.addCoinTip();//调用UI弹出收租提示
					break;
				case EnumBuildState.SUPPLYSERVICES_STATE://提供服务
					tipFrame="business_served";
					break;
				case EnumBuildState.SUPPLYGOODS_STATE://供货
					tipFrame = "business_supply";
					m_building.addGoodsTip();
					break;
				case EnumBuildState.IDLE_STATE://闲置状态
					tipFrame = "idle";
					//2.设置
					m_building.setIdleUI();
					GlobalTimer.setFuncRun(m_userbvo.buildingid,false);
					break;
			
			}
		}
	}
}